package com.zou.controller.backstage.user;

import com.zou.pojo.SysUser;
import com.zou.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/backstage/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    public String list(SysUser sysUser, Model model) {
        List<SysUser> userList = sysUserService.querySomeSysUserBySysUser(sysUser);
        for (SysUser user : userList) {
            user.setPassword("******");
        }
        model.addAttribute("userList", userList);
        return "backstage/user/list";
    }

    @GetMapping("/add")
    public String add() {
        return "backstage/user/add";
    }

    @PostMapping("/add")
    public @ResponseBody
    Object add(SysUser sysUser) {
        return sysUserService.addSysUser(sysUser);
    }

    @PostMapping("/del")
    public @ResponseBody
    Object del(Integer id) {
        return sysUserService.delSysUserById(id);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        SysUser sysUser = sysUserService.querySysUserById(id);
        model.addAttribute("user", sysUser);
        return "backstage/user/edit";
    }

    @PostMapping("/edit")
    public @ResponseBody
    Object edit(SysUser sysUser) {
        return sysUserService.editSysUser(sysUser);
    }

    @PostMapping("/edit/state")
    public @ResponseBody
    Object editState(String state, String id) {
        return sysUserService.editSysUserStateById(state, id);
    }
}
